

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #    填写   企业参与国家标准或行业标准制定情况汇总表
from util.selenium.webdriver.common.by import By
from base.BaseMethod import BM
from util.DataQuery.FormDataDecorator import FormDataDecorator
from util.g import g


class WriteStandForm():
    form_btn_loc = (By.LINK_TEXT, '企业参与国家标准或行业标准制定情况汇总表')
    content_iframe_loc = (By.CSS_SELECTOR, "#innocomFrame")
    #    添加按钮
    # add_btn_loc = (By.CSS_SELECTOR, '.form-group.pull-right .blue')
    add_btn_loc = (By.XPATH, "//div[@class='form-group pull-right']/button[contains(string(), '添加')]")
    #    删除按钮
    # delete_btn_loc = (By.CSS_SELECTOR, '.table-search div:nth-of-type(2) button')
    delete_btn_loc = (By.XPATH, "//div[@class='form-group pull-right']/button[contains(string(), '删除')]")
    standard_name_loc = (By.CSS_SELECTOR, 'input#bname')
    standard_num_loc = (By.CSS_SELECTOR, 'input#bnumber')
    # 标准级别 字典值 standard_level
    level = ''
    level_css_str = 'input#dataEprTotalForm_dataEprTotal_blevel'
    # 参与方式 字典值 prtp_mode
    way = ''
    way_css_str = 'input#dataEprTotalForm_dataEprTotal_pway'
    # level_loc = (By.CSS_SELECTOR, level_css_str + level)
    # way_loc = (By.CSS_SELECTOR, way_css_str + way)
    upload_btn_loc = (By.CSS_SELECTOR, '.uploadAttachment a')
    back_btn_loc = (By.CSS_SELECTOR, '.box-footer.text-right button.btn-default')
    save_btn_loc = (By.CSS_SELECTOR, '.box-footer.text-right button#dataEprTotalsave')
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 暂时不使用
    # 国家级别
    conutry_level_loc = (By.CSS_SELECTOR, 'input#dataEprTotalForm_dataEprTotal_blevel国家')
    # 行业级别
    industry_level_loc = (By.CSS_SELECTOR, 'input#dataEprTotalForm_dataEprTotal_blevel行业')
    # 参与方式 主持
    preside_over_style_loc = (By.CSS_SELECTOR, 'input#dataEprTotalForm_dataEprTotal_pway主持')
    # 参与方式 参与
    participation_style_loc = (By.CSS_SELECTOR, 'input#dataEprTotalForm_dataEprTotal_pway参与')
    ##DataEprTotal_list > div.eXtremeTable > div.page > ul > li:nth-child(4) > a
    next_page_loc = (By.CSS_SELECTOR, ".pagination.pull-right li:nth-of-type(4) a")

    page_num_loc = (By.CSS_SELECTOR, ".pagination.pull-right li:nth-of-type(3) a")
    select_all_loc = (By.CSS_SELECTOR, "input#items_selector")

    delete = False
    # 写代码时方便提示, 基本方法完成后删除
    # driver = driver
    def __init__(self, driver=None):
        self.driver = driver
        setattr(g, self.class_name, list())

    def get_level_loc(self):
        return (By.CSS_SELECTOR, self.level_css_str + self.level)

    def get_way_loc(self):
        return (By.CSS_SELECTOR, self.way_css_str + self.way)

    @property
    def class_name(self):
        return self.__class__.__name__

    def click_save_btn(self):
        return BM.global_click_btn(btn_loc=self.save_btn_loc, driver=self.driver)

    @FormDataDecorator
    def main(self, id='', stand_name='', stand_num='', stand_level='', stand_way='', oss_path=None):

        stand_name = stand_name.replace(" ", "")
        stand_num = stand_num.replace(" ", "")
        # if g.is_first_entry and self.delete:
        #     BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        #     BM.delete_all(driver=self.driver, page_num_loc=self.page_num_loc, select_all_loc=self.select_all_loc, delete_btn_loc=self.delete_btn_loc)
        #     self.delete = True
        is_click = False
        class_name = self.class_name
        if getattr(g, class_name) == []:
            is_click = True
            BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
            BM.cut_over_frame(driver=self.driver, selector_loc=self.content_iframe_loc)
            a_list = getattr(g, class_name)
            table_loc = (By.CSS_SELECTOR, ".tableBody")
            tr_loc = (By.CSS_SELECTOR, ".tableBody tr td:nth-of-type(5)")

            while True and g.repetition:
                BM.wait(driver=self.driver, ele_loc=table_loc)
                data = self.driver.find_elements(*tr_loc)
                for i in data:
                    # 获取 标签中的 汉字
                    sta_name_str = i.text
                    a_list.append(sta_name_str)

                next_btn = BM.get_ele(driver=self.driver, ele_loc=self.next_page_loc)
                onclick = next_btn.get_attribute("onclick")
                if not onclick:
                    # BM.click(driver=self.driver, ele_loc=self.next_page_loc)
                    break
                else:
                    next_btn.click()
        li_list = getattr(g, class_name)
        if stand_num in li_list and g.repetition:
            g.logger.info("{} ----- {} 已存在".format(class_name, stand_num))
            return
        g.logger.info("{}： {}".format(class_name, li_list))

        # if not is_click:
        #     BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        # if g.repetition and not self.delete:
        #     BM.delete_all(driver=self.driver, page_num_loc=self.page_num_loc, select_all_loc=self.select_all_loc, delete_btn_loc=self.delete_btn_loc)
        #     self.delete = True

        BM.click(driver=self.driver, ele_loc=self.add_btn_loc)
        BM.wait(driver=self.driver, ele_loc=self.save_btn_loc)
        # 填写  企业参与国家标准或行业标准制定情况汇总表  信息
        self.level = stand_level
        self.way = stand_way
        BM.input(ele_loc=self.standard_name_loc, driver=self.driver, keys=stand_name)
        # stand_num = stand_num.replace(" ", "")
        BM.input(ele_loc=self.standard_num_loc, driver=self.driver, keys=stand_num)
        BM.click(driver=self.driver, ele_loc=self.get_level_loc())
        BM.click(driver=self.driver, ele_loc=self.get_way_loc())
        BM.click_upload_attach(btn_loc=self.upload_btn_loc, driver=self.driver, class_name=self.class_name, _id=id)
        res = self.click_save_btn()
        if res:
            li_list.append(stand_num)

        # click_back_btn(btn_loc=self.back_btn_loc, driver=self.driver)
